<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>评论管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <el-table :data="articles" style="width: 100%">
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column prop="total_comment_count" label="总评论数">
        </el-table-column>
        <el-table-column prop="fans_comment_count" label="粉丝评论数">
        </el-table-column>
        <el-table-column prop="comment_status" label="状态">
          <template slot-scope="scope">
            {{ scope.row.comment_status ? "正常" : "关闭" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.comment_status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="onStatusChange(scope.row)"
              :disabled="scope.row.statusLoading"
            >
            </el-switch>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-sizes="[10, 20, 50, 60]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      />
    </el-card>
  </div>
</template>

<script>
import { getArticles, updateArticleCommentsStatus } from '@/api/article'
export default {
  name: 'CommentIndex',
  data () {
    return {
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }
      ],
      currentPage: 1,
      pageSize: 10,
      articles: [],
      totalCount: 0
    }
  },
  props: {},
  components: {},
  mounted () {},
  created () {
    this.loadArticlesData()
  },
  methods: {
    handleSizeChange (e) {
      this.pageSize = e
      this.loadArticlesData()
    },
    handleCurrentChange (e) {
      this.page = e
      this.loadArticlesData()
    },
    loadArticlesData () {
      const params = {
        response_type: 'comment',
        page: this.page,
        per_page: this.pageSize
      }
      getArticles(params).then((res) => {
        const { results, total_count: totalCount } = res.data.data
        results.forEach((element) => {
          element.statusLoading = false
        })
        this.articles = results
        this.totalCount = totalCount
      })
    },
    // switch改变事件
    onStatusChange (item) {
      item.statusLoading = true
      updateArticleCommentsStatus(item.id, item.comment_status)
        .then((res) => {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          item.statusLoading = false
        })
        .catch((e) => {
          item.statusLoading = false
        })
    }
  }
}
</script>

<style scoped>
</style>
